package com.yungu.swift.admin.route.marketing;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.admin.consts.AdminErrorEnum;
import com.yungu.swift.admin.utils.ExcelUtil;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.PageVo;
import com.yungu.swift.common.model.dto.CommonFeedbackDto;
import com.yungu.swift.common.model.param.AdminFeedbackPageParam;
import com.yungu.swift.common.model.param.AdminFeedbackReplyParam;
import com.yungu.swift.common.service.CommonFeedbackService;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.ParamUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author : cuixiuyin
 * @date : 2019/6/20
 */
@RestController
@RequestMapping("/admin/v1/marketing/feedback")
@Api(value = "【营销管理】", tags = "反馈管理")
public class FeedbackAdmin {

    @Reference
    private CommonFeedbackService commonFeedbackService;

    @RequestMapping(value = "/queryPage", method = RequestMethod.POST)
    @ApiOperation(value = "反馈信息分页查询", httpMethod = "POST")
    public ResponseData<PageVo<CommonFeedbackDto>> queryPage(@RequestBody AdminFeedbackPageParam feedbackPageParam) {
        return commonFeedbackService.queryPage(feedbackPageParam);
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ApiOperation(value = "反馈信息详情", httpMethod = "GET")
    public ResponseData<CommonFeedbackDto> detail(@RequestParam String uuid) {
        if (ParamUtil.isIllegal(uuid)) {
            return ResponseData.buildErrorResponse(AdminErrorEnum.PARAM_ERROR.getCode(), AdminErrorEnum.PARAM_ERROR.getMsg());
        }
        return commonFeedbackService.get(uuid);
    }

    @RequestMapping(value = "/reply", method = RequestMethod.POST)
    @ApiOperation(value = "反馈信息回复", httpMethod = "POST")
    public ResponseData<Boolean> uploadImage(@RequestBody AdminFeedbackReplyParam feedbackReplyParam) {
        if (ParamUtil.isIllegal(feedbackReplyParam.getUuid())) {
            return ResponseData.buildErrorResponse(AdminErrorEnum.PARAM_ERROR.getCode(), AdminErrorEnum.PARAM_ERROR.getMsg());
        }
        CommonFeedbackDto feedbackDto = new CommonFeedbackDto();
        BeanUtils.copyProperties(feedbackReplyParam, feedbackDto);
        feedbackDto.setStatus(CommonFeedbackDto.STATUS_REPLYED);
        feedbackDto.setUpdateBy(feedbackReplyParam.getUserUuid());
        return commonFeedbackService.edit(feedbackDto);
    }

    @RequestMapping(value = "/export", method = RequestMethod.POST)
    @ApiOperation(value = "反馈信息导出", httpMethod = "POST")
    public void export(@RequestBody AdminFeedbackPageParam feedbackPageParam, HttpServletResponse response) {
        String templatePath = "/template/feedback.xlsx";
        List<CommonFeedbackDto> list = commonFeedbackService.list(MapUtils.convertObjToMap(feedbackPageParam)).getData();
        ExcelUtil.export(response, list, CommonFeedbackDto.class, templatePath, ExcelUtil.getFileName("反馈"), null, "yyy/MM/dd hh:mm:ss");
    }
}
